package com.aigame.dao;

import android.content.ContentValues;
import android.content.Context;
import android.database.Cursor;

import com.aigame.entity.User;
import com.aigame.util.DBHelper;

public class UserDao {
	private DBHelper dbHelper;
	private User user = null;
	public UserDao(Context c) {
		this.dbHelper = new DBHelper(c);
	}
	public void updateUserLogin(User user) {
		try {
			ContentValues values = new ContentValues();
			values.put("uid", user.getUid());
			values.put("username", user.getUsername());
			values.put("password", user.getPassword());
			values.put("desc", "lastLoginAccount");
			values.put("sid", user.getSid());
			ContentValues values1 = new ContentValues();
			values1.put("desc", "");
			dbHelper.update(values1, "desc", "lastLoginAccount");
			if (dbHelper.queryBy("username", user.getUsername())
					.getCount() == 0) {
				dbHelper.insert(values);
			} else {
				dbHelper.update(values, "username", user.getUsername());
			}
		} finally {
			dbHelper.close();
		}
	}
	public User queryLastLogin() {
		try {
			Cursor c = dbHelper.queryBy("desc","lastLoginAccount");
			if(c.getCount() > 0) {
				c.moveToFirst();
				String username = c.getString(c.getColumnIndex("username"));
				String password = c.getString(c.getColumnIndex("password"));
				user = new User();
				user.setUsername(username);
				user.setPassword(password);
			}
			return user;
		} finally {
			dbHelper.close();
		}
		
	}
	public void cleanLoginHistory() {
		try {
			dbHelper.delAll();
		} finally {
			dbHelper.close();
		}
		
	}
	
}
